<template>
  <div class="modal-overlay" v-if="visible" @click="handleOverlayClick">
    <div class="modal-content" :class="modalClass" @click.stop>
      <div class="modal-header">
        <h3 class="modal-title">{{ title }}</h3>
        <button class="modal-close" @click="handleClose" v-if="showClose">×</button>
      </div>
      <div class="modal-body">
        <p v-if="message" class="modal-message">{{ message }}</p>
        <slot></slot>
      </div>
      <div class="modal-footer">
        <button 
          v-if="showCancel" 
          class="modal-btn modal-btn-cancel" 
          @click="handleCancel">
          {{ cancelText }}
        </button>
        <button 
          v-if="showConfirm" 
          class="modal-btn modal-btn-confirm" 
          :class="confirmClass" 
          @click="handleConfirm">
          {{ confirmText }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: ''
  },
  message: {
    type: String,
    default: ''
  },
  confirmText: {
    type: String,
    default: '确认'
  },
  cancelText: {
    type: String,
    default: '取消'
  },
  showClose: {
    type: Boolean,
    default: true
  },
  showCancel: {
    type: Boolean,
    default: true
  },
  showConfirm: {
    type: Boolean,
    default: true
  },
  type: {
    type: String,
    default: 'default', // default, danger, warning, info
  },
  closeOnOverlay: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['update:visible', 'confirm', 'cancel', 'close'])

const modalClass = computed(() => `modal-${props.type}`)
const confirmClass = computed(() => props.type === 'danger' ? 'modal-btn-danger' : '')

const handleClose = () => {
  emit('update:visible', false)
  emit('close')
}

const handleCancel = () => {
  emit('update:visible', false)
  emit('cancel')
}

const handleConfirm = () => {
  emit('confirm')
}

const handleOverlayClick = () => {
  if (props.closeOnOverlay) {
    handleClose()
  }
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3000;
  backdrop-filter: blur(2px);
}

.modal-content {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  min-width: 400px;
  max-width: 500px;
  max-height: 80vh;
  overflow: hidden;
  animation: modal-appear 0.2s ease-out;
}

@keyframes modal-appear {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 16px;
  border-bottom: 1px solid #f0f0f0;
}

.modal-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #262626;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #8c8c8c;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: all 0.2s;
}

.modal-close:hover {
  background: #f5f5f5;
  color: #595959;
}

.modal-body {
  padding: 24px;
}

.modal-message {
  margin: 0;
  font-size: 16px;
  line-height: 1.5;
  color: #595959;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 24px 20px;
  border-top: 1px solid #f0f0f0;
}

.modal-btn {
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid;
}

.modal-btn-cancel {
  background: white;
  border-color: #d9d9d9;
  color: #595959;
}

.modal-btn-cancel:hover {
  border-color: #40a9ff;
  color: #40a9ff;
}

.modal-btn-confirm {
  background: #1890ff;
  border-color: #1890ff;
  color: white;
}

.modal-btn-confirm:hover {
  background: #40a9ff;
  border-color: #40a9ff;
}

.modal-btn-danger {
  background: #ff4d4f;
  border-color: #ff4d4f;
  color: white;
}

.modal-btn-danger:hover {
  background: #ff7875;
  border-color: #ff7875;
}

.modal-danger {
  border-left: 4px solid #ff4d4f;
}

.modal-warning {
  border-left: 4px solid #faad14;
}

.modal-info {
  border-left: 4px solid #1890ff;
}

@media (max-width: 480px) {
  .modal-content {
    margin: 16px;
    min-width: auto;
    max-width: none;
  }
  
  .modal-footer {
    flex-direction: column;
    gap: 8px;
  }
  
  .modal-btn {
    width: 100%;
  }
}
</style>